package org.soenea.doitall.application.filter;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class BuddyAgePermalinkFilter implements Filter {

	@Override
	public void destroy() {
		// Nothing to do.
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		String command = getCommandFromPathInfoAndMaybeSetSomeAttributes((HttpServletRequest)request);
		if (request.getParameter("command") == null && command != null) {
			request.setAttribute("command", command);
		}
		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig fConfig) throws ServletException {
		// Nothing to do.
	}

	/**
	 * Attempt to extract the command from the request's pathInfo. Note that
	 * this method might set request attributes.
	 * 
	 * @param request
	 * @return null or the command as a simple class name (i.e. not fully
	 *         qualified with the package).
	 */
	private String getCommandFromPathInfoAndMaybeSetSomeAttributes(
			HttpServletRequest request) {
		String path = request.getPathInfo();
		if (path == null)
			return null;

		String command = null;
		if ("/buddy".equals(path)) {
			command = "BrowsePeople";
		} 
		else if ("/groups".equals(path)){
			command = "BrowseGroup";
		}
		else if("/invites".equals(path)){
			command = "BrowseInvite";
			}
		else if("/login".equals(path)){
			command = "LoginDispatcher";
			}
		else if("/logout".equals(path)){
			command = "LogoutDispatcher";
			}
		else {
			Pattern pattern = Pattern.compile("^/buddy/(\\d+)$");
			Pattern groupPattern = Pattern.compile("^/group/(\\d+)$");
			Pattern invitePattern = Pattern.compile("^/invite/(\\d+)$");
			Pattern inviteUserPattern = Pattern.compile("^/inviteUser/(\\d+)/(\\d+)$");
			Matcher matcher = pattern.matcher(path);
			if (matcher.find()) {
				String idAsString = null;
				command = "ViewPerson";
				idAsString = matcher.group(1);
				try {
					long id = Long.parseLong(idAsString);
					request.setAttribute("id", id);
				} catch (NumberFormatException e) {
					return null;
				}
			}
			//showing groups
			matcher = groupPattern.matcher(path);
			if(matcher.find()){
				String idAsString = null;
				command = "ViewGroup";
				idAsString = matcher.group(1);
				try {
					long id = Long.parseLong(idAsString);
					request.setAttribute("id", id);
				} catch (NumberFormatException e) {
					return null;
				}
			}
			//showing invite details
			matcher = invitePattern.matcher(path);
			if(matcher.find()){
				String idAsString = null;
				command = "ViewInvite";
				idAsString = matcher.group(1);
				try {
					long id = Long.parseLong(idAsString);
					request.setAttribute("id", id);
				} catch (NumberFormatException e) {
					return null;
				}
			}
			//showing invite details
			matcher = inviteUserPattern.matcher(path);
			if(matcher.find()){
				String idAsString = null;
				String userIdAsString = null;
				command = "InviteUser";
				idAsString = matcher.group(2);
				userIdAsString = matcher.group(1);
				try {
					long id = Long.parseLong(idAsString);
					long userID = Long.parseLong(userIdAsString);
					request.setAttribute("id", id);
					request.setAttribute("invitedID",userID);
				} catch (NumberFormatException e) {
					return null;
				}
			}
			
		}
		return command;
	}

}
